/*
 * GeneralOptionPanel.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.settings.gui;

import java.util.ResourceBundle;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.tiberiumlabs.lailaps.gui.elements.ExceptionDialog;
import org.tiberiumlabs.lailaps.settings.ApplicationSettings;
import org.tiberiumlabs.lailaps.settings.ApplicationSettingsImpl;
import org.tiberiumlabs.lailaps.settings.Settings;
import org.tiberiumlabs.lailaps.toolkit.Constants;
import org.tiberiumlabs.lailaps.settings.helpers.IntegrationHelper;

/**
 *
 * @author <a href="mailto:beast.tiberiumlabs@gmail.com">Beast</a>
 */
public class GeneralOptionPanel extends javax.swing.JPanel implements  OptionPanel {
    
    private static final String D_SAVE_LIST_FILE = "Choose list file...";
    private static final String D_SAVE_OUTPUT_FOLDER = "Choose output folder...";
    
    ApplicationSettings appSettings = Settings.getApplicationSettings();
    ApplicationSettingsImpl appSettingsWriter = Settings.getEditableApplicationSettings();
    
    private ResourceBundle bundle = ResourceBundle.getBundle("org/tiberiumlabs/lailaps/resources/messages/settings_general");
    
    /** Creates new form GeneralOptionPanel */
    public GeneralOptionPanel() {
        initComponents();
        languageComboBox.addItem(Constants.ENGLISH_LNG);
        languageComboBox.addItem(Constants.RUSSIAN_LNG);
        languageComboBox.addItem(Constants.UKRAINIAN_LNG);
        languageComboBox.setSelectedIndex(0);
        readEnvSettings();
        readBehaviourSettings();
        readLanguageSettings();
        readIntegrationSettings();
        readHistorySettings();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        enviromentPanel = new javax.swing.JPanel();
        settingsPanel = new javax.swing.JPanel();
        settingsDirTextField = new javax.swing.JTextField();
        listPanel = new javax.swing.JPanel();
        listFileTextField = new javax.swing.JTextField();
        browseListFile = new org.tiberiumlabs.lailaps.gui.elements.OpenButton();
        outputPanel = new javax.swing.JPanel();
        outputFolderField = new javax.swing.JTextField();
        browseOutputFolder = new org.tiberiumlabs.lailaps.gui.elements.OpenButton();
        behaviourPanel = new javax.swing.JPanel();
        removeFinishedCheckBox = new javax.swing.JCheckBox();
        keepServerTimeCheckBox = new javax.swing.JCheckBox();
        askOnExitCheckBox = new javax.swing.JCheckBox();
        portLabel = new javax.swing.JLabel();
        portTextField = new javax.swing.JTextField();
        askOnDeleteCheckBox = new javax.swing.JCheckBox();
        autostartDownloadCheckBox = new javax.swing.JCheckBox();
        silentDropCheckBox = new javax.swing.JCheckBox();
        addToFrontCheckBox = new javax.swing.JCheckBox();
        backupListCheckBox = new javax.swing.JCheckBox();
        alowDuplicatesCheckBox = new javax.swing.JCheckBox();
        historyPanel = new javax.swing.JPanel();
        enableHistoryCheckBox = new javax.swing.JCheckBox();
        historyTimespanLabel = new javax.swing.JLabel();
        historyTimespanSpinner = new javax.swing.JSpinner();
        historySizeLimitLabel = new javax.swing.JLabel();
        historySizeLimitSpinner = new javax.swing.JSpinner();
        notifyIfFoundInHitoryCheckBox = new javax.swing.JCheckBox();
        historyDaysLabel = new javax.swing.JLabel();
        historyKilobytesLabel = new javax.swing.JLabel();
        noLimitLabel1 = new javax.swing.JLabel();
        noLimitLabel2 = new javax.swing.JLabel();
        languagePanel = new javax.swing.JPanel();
        useSystemLocaleCheckBox = new javax.swing.JCheckBox();
        customLanguageLabel = new javax.swing.JLabel();
        languageComboBox = new javax.swing.JComboBox();
        integrationPanel = new javax.swing.JPanel();
        interceptFromPanel = new javax.swing.JPanel();
        konquerorImage = new javax.swing.JLabel();
        konquerorCheckBox = new javax.swing.JCheckBox();
        firefoxImage = new javax.swing.JLabel();
        firefoxCheckBox = new javax.swing.JCheckBox();

        jTabbedPane1.setFont(new java.awt.Font("Dialog", 0, 12));
        jTabbedPane1.setPreferredSize(new java.awt.Dimension(0, 0));

        enviromentPanel.setFont(new java.awt.Font("Dialog", 0, 10));

        settingsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("settingsPanel.title"))); // NOI18N

        settingsDirTextField.setEnabled(false);

        javax.swing.GroupLayout settingsPanelLayout = new javax.swing.GroupLayout(settingsPanel);
        settingsPanel.setLayout(settingsPanelLayout);
        settingsPanelLayout.setHorizontalGroup(
            settingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(settingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(settingsDirTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                .addContainerGap())
        );
        settingsPanelLayout.setVerticalGroup(
            settingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(settingsPanelLayout.createSequentialGroup()
                .addComponent(settingsDirTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        listPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("listPanel.title"))); // NOI18N

        browseListFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseListFileActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout listPanelLayout = new javax.swing.GroupLayout(listPanel);
        listPanel.setLayout(listPanelLayout);
        listPanelLayout.setHorizontalGroup(
            listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, listPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listFileTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(browseListFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        listPanelLayout.setVerticalGroup(
            listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(listPanelLayout.createSequentialGroup()
                .addGroup(listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(browseListFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(listFileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        outputPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("outputPanel.title"))); // NOI18N

        browseOutputFolder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseOutputFolderActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout outputPanelLayout = new javax.swing.GroupLayout(outputPanel);
        outputPanel.setLayout(outputPanelLayout);
        outputPanelLayout.setHorizontalGroup(
            outputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, outputPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(outputFolderField, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(browseOutputFolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        outputPanelLayout.setVerticalGroup(
            outputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(outputPanelLayout.createSequentialGroup()
                .addGroup(outputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(browseOutputFolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(outputFolderField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout enviromentPanelLayout = new javax.swing.GroupLayout(enviromentPanel);
        enviromentPanel.setLayout(enviromentPanelLayout);
        enviromentPanelLayout.setHorizontalGroup(
            enviromentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, enviromentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(enviromentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(outputPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(listPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(settingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        enviromentPanelLayout.setVerticalGroup(
            enviromentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(enviromentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(settingsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(listPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(outputPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(70, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(bundle.getString("enviromentPanel.title"), enviromentPanel); // NOI18N

        removeFinishedCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        removeFinishedCheckBox.setText(bundle.getString("removeFinishedCheckBox")); // NOI18N
        removeFinishedCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        removeFinishedCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        keepServerTimeCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        keepServerTimeCheckBox.setText(bundle.getString("keepServerTimeCheckBox")); // NOI18N
        keepServerTimeCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        keepServerTimeCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        askOnExitCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        askOnExitCheckBox.setText(bundle.getString("askOnExitCheckBox")); // NOI18N
        askOnExitCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        askOnExitCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        portLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        portLabel.setText(bundle.getString("portLabel")); // NOI18N

        askOnDeleteCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        askOnDeleteCheckBox.setText(bundle.getString("askOnDeleteCheckBox")); // NOI18N
        askOnDeleteCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        askOnDeleteCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        autostartDownloadCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        autostartDownloadCheckBox.setText(bundle.getString("autostartDownloadCheckBox")); // NOI18N
        autostartDownloadCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        autostartDownloadCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        silentDropCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        silentDropCheckBox.setText(bundle.getString("silentDropCheckBox")); // NOI18N
        silentDropCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        silentDropCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        addToFrontCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        addToFrontCheckBox.setText(bundle.getString("addToFrontCheckBox")); // NOI18N
        addToFrontCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        addToFrontCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        backupListCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        backupListCheckBox.setText(bundle.getString("backupListCheckBox")); // NOI18N
        backupListCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        backupListCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        alowDuplicatesCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        alowDuplicatesCheckBox.setText(bundle.getString("alowDuplicatesCheckBox")); // NOI18N
        alowDuplicatesCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        alowDuplicatesCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout behaviourPanelLayout = new javax.swing.GroupLayout(behaviourPanel);
        behaviourPanel.setLayout(behaviourPanelLayout);
        behaviourPanelLayout.setHorizontalGroup(
            behaviourPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(behaviourPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(behaviourPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(removeFinishedCheckBox)
                    .addComponent(keepServerTimeCheckBox)
                    .addComponent(askOnExitCheckBox)
                    .addGroup(behaviourPanelLayout.createSequentialGroup()
                        .addComponent(portLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(portTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(askOnDeleteCheckBox)
                    .addComponent(autostartDownloadCheckBox)
                    .addComponent(silentDropCheckBox)
                    .addComponent(addToFrontCheckBox)
                    .addComponent(backupListCheckBox)
                    .addComponent(alowDuplicatesCheckBox))
                .addContainerGap(85, Short.MAX_VALUE))
        );
        behaviourPanelLayout.setVerticalGroup(
            behaviourPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(behaviourPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(behaviourPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(portLabel)
                    .addComponent(portTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(removeFinishedCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(keepServerTimeCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(askOnExitCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(askOnDeleteCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(autostartDownloadCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(silentDropCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addToFrontCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(backupListCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(alowDuplicatesCheckBox)
                .addContainerGap(30, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(bundle.getString("behaviourPanel.title"), behaviourPanel); // NOI18N

        enableHistoryCheckBox.setSelected(true);
        enableHistoryCheckBox.setText(bundle.getString("enableHistoryCheckBox")); // NOI18N
        enableHistoryCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enableHistoryCheckBoxActionPerformed(evt);
            }
        });

        historyTimespanLabel.setText(bundle.getString("historyTimespanLabel")); // NOI18N

        historySizeLimitLabel.setText(bundle.getString("historySizeLimitLabel")); // NOI18N

        notifyIfFoundInHitoryCheckBox.setText(bundle.getString("notifyIfFoundInHitoryCheckBox")); // NOI18N

        historyDaysLabel.setText(bundle.getString("historyDaysLabel")); // NOI18N

        historyKilobytesLabel.setText(bundle.getString("historyKilobytesLabel")); // NOI18N

        noLimitLabel1.setText(bundle.getString("noLimitLabel")); // NOI18N

        noLimitLabel2.setText(bundle.getString("noLimitLabel")); // NOI18N

        javax.swing.GroupLayout historyPanelLayout = new javax.swing.GroupLayout(historyPanel);
        historyPanel.setLayout(historyPanelLayout);
        historyPanelLayout.setHorizontalGroup(
            historyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(historyPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(historyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(historyPanelLayout.createSequentialGroup()
                        .addComponent(notifyIfFoundInHitoryCheckBox, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(historyPanelLayout.createSequentialGroup()
                        .addGroup(historyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(enableHistoryCheckBox)
                            .addGroup(historyPanelLayout.createSequentialGroup()
                                .addGroup(historyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(historyTimespanLabel)
                                    .addComponent(historySizeLimitLabel))
                                .addGap(21, 21, 21)
                                .addGroup(historyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(historyTimespanSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(historySizeLimitSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(historyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(historyPanelLayout.createSequentialGroup()
                                .addComponent(historyKilobytesLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(noLimitLabel2))
                            .addGroup(historyPanelLayout.createSequentialGroup()
                                .addComponent(historyDaysLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(noLimitLabel1)))
                        .addGap(110, 110, 110))))
        );
        historyPanelLayout.setVerticalGroup(
            historyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(historyPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(enableHistoryCheckBox)
                .addGap(8, 8, 8)
                .addGroup(historyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(historyDaysLabel)
                    .addComponent(historyTimespanSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(noLimitLabel1)
                    .addComponent(historyTimespanLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(historyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(historyKilobytesLabel)
                    .addComponent(noLimitLabel2)
                    .addComponent(historySizeLimitSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(historySizeLimitLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(notifyIfFoundInHitoryCheckBox)
                .addContainerGap(153, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(bundle.getString("historyPanel.title"), historyPanel); // NOI18N

        useSystemLocaleCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        useSystemLocaleCheckBox.setText(bundle.getString("useSystemLocaleCheckBox")); // NOI18N
        useSystemLocaleCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        useSystemLocaleCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        useSystemLocaleCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                useSystemLocaleCheckBoxActionPerformed(evt);
            }
        });

        customLanguageLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        customLanguageLabel.setText(bundle.getString("customLanguageLabel")); // NOI18N

        languageComboBox.setFont(new java.awt.Font("Dialog", 0, 12));
        languageComboBox.setEnabled(false);

        javax.swing.GroupLayout languagePanelLayout = new javax.swing.GroupLayout(languagePanel);
        languagePanel.setLayout(languagePanelLayout);
        languagePanelLayout.setHorizontalGroup(
            languagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(languagePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(languagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(useSystemLocaleCheckBox)
                    .addGroup(languagePanelLayout.createSequentialGroup()
                        .addComponent(customLanguageLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(languageComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(158, Short.MAX_VALUE))
        );
        languagePanelLayout.setVerticalGroup(
            languagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(languagePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(useSystemLocaleCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(languagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(customLanguageLabel)
                    .addComponent(languageComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(208, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(bundle.getString("languagePanel.title"), languagePanel); // NOI18N

        interceptFromPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("interceptFromPanel"))); // NOI18N

        konquerorImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/tiberiumlabs/lailaps/resources/images/browsers/konqueror.png"))); // NOI18N
        konquerorImage.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 0, 1, 0));
        konquerorImage.setFocusable(false);

        konquerorCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        konquerorCheckBox.setText("Konqueror");
        konquerorCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        konquerorCheckBox.setIconTextGap(5);
        konquerorCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        firefoxImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/tiberiumlabs/lailaps/resources/images/browsers/firefox.png"))); // NOI18N
        firefoxImage.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 0, 1, 0));
        firefoxImage.setFocusable(false);

        firefoxCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        firefoxCheckBox.setText("Firefox");
        firefoxCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        firefoxCheckBox.setEnabled(false);
        firefoxCheckBox.setIconTextGap(5);
        firefoxCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout interceptFromPanelLayout = new javax.swing.GroupLayout(interceptFromPanel);
        interceptFromPanel.setLayout(interceptFromPanelLayout);
        interceptFromPanelLayout.setHorizontalGroup(
            interceptFromPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(interceptFromPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(interceptFromPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(interceptFromPanelLayout.createSequentialGroup()
                        .addComponent(konquerorImage)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(konquerorCheckBox))
                    .addGroup(interceptFromPanelLayout.createSequentialGroup()
                        .addComponent(firefoxImage)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(firefoxCheckBox)))
                .addContainerGap(283, Short.MAX_VALUE))
        );
        interceptFromPanelLayout.setVerticalGroup(
            interceptFromPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(interceptFromPanelLayout.createSequentialGroup()
                .addGroup(interceptFromPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(interceptFromPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(konquerorImage))
                    .addGroup(interceptFromPanelLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(konquerorCheckBox)))
                .addGroup(interceptFromPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(interceptFromPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(firefoxImage))
                    .addGroup(interceptFromPanelLayout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(firefoxCheckBox)))
                .addContainerGap(133, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout integrationPanelLayout = new javax.swing.GroupLayout(integrationPanel);
        integrationPanel.setLayout(integrationPanelLayout);
        integrationPanelLayout.setHorizontalGroup(
            integrationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(integrationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(interceptFromPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        integrationPanelLayout.setVerticalGroup(
            integrationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, integrationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(interceptFromPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab(bundle.getString("integrationPanel.title"), integrationPanel); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void useSystemLocaleCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_useSystemLocaleCheckBoxActionPerformed
        languageComboBox.setEnabled(!useSystemLocaleCheckBox.isSelected());
    }//GEN-LAST:event_useSystemLocaleCheckBoxActionPerformed
    
    private void browseOutputFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseOutputFolderActionPerformed
        JFileChooser fc = new JFileChooser(outputFolderField.getText());
        fc.setDialogTitle(D_SAVE_OUTPUT_FOLDER);
        fc.setDialogType(JFileChooser.SAVE_DIALOG);
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.setAcceptAllFileFilterUsed(false);
        
        int res = fc.showSaveDialog(outputFolderField);
        if (res == JFileChooser.APPROVE_OPTION) {
            try {
                outputFolderField.setText(fc.getSelectedFile().getCanonicalPath());
            } catch (Exception ex) {
                new ExceptionDialog(ex);
            }
        }
    }//GEN-LAST:event_browseOutputFolderActionPerformed
    
    private void browseListFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseListFileActionPerformed
        String currentDir = listFileTextField.getText();
        JFileChooser fc = new JFileChooser(currentDir.substring(0, currentDir.lastIndexOf(appSettings.FILESEPARATOR)));
        fc.setDialogTitle(D_SAVE_LIST_FILE);
        fc.setDialogType(JFileChooser.SAVE_DIALOG);
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        FileNameExtensionFilter gzFilter = new FileNameExtensionFilter("XML in GZ archive (usually faster than plain XML)", "gz");
        fc.setAcceptAllFileFilterUsed(false);
        fc.addChoosableFileFilter(gzFilter);
        
        int res = fc.showSaveDialog(listFileTextField);
        if (res == JFileChooser.APPROVE_OPTION) {
            try {
                listFileTextField.setText(fc.getSelectedFile().getCanonicalPath());
            } catch (Exception ex) {
                new ExceptionDialog(ex);
            }
        }
    }//GEN-LAST:event_browseListFileActionPerformed

    private void enableHistoryCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enableHistoryCheckBoxActionPerformed
         boolean status = enableHistoryCheckBox.isSelected();
         historyTimespanLabel.setEnabled(status);
         historyTimespanSpinner.setEnabled(status);
         historyKilobytesLabel.setEnabled(status);
         historySizeLimitLabel.setEnabled(status);
         historySizeLimitSpinner.setEnabled(status);
         historyDaysLabel.setEnabled(status);
         notifyIfFoundInHitoryCheckBox.setEnabled(status);
         noLimitLabel1.setEnabled(status);//GEN-LAST:event_enableHistoryCheckBoxActionPerformed
         noLimitLabel2.setEnabled(status);
    }                                                     
    
    public void applyAction() {
        saveEnvSettings();
        saveBehaviourSettings();
        saveLanguageSettings();
        saveIntegraionSettings();
        saveHistorySettings();
        Settings.saveApplicationSettings();
    }
    
    public void okAction() {
        applyAction();
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Internal methods for reading and saving settings ">
    
    public void readEnvSettings() {
        settingsDirTextField.setText(appSettings.SETTINGS_FOLDER);
        settingsDirTextField.setEnabled(false);
        listFileTextField.setText(appSettings.getListFile());
        outputFolderField.setText(appSettings.getOutputFolder());
    }
    
    public void saveEnvSettings() {
        appSettingsWriter.setListFile(listFileTextField.getText());
        appSettingsWriter.setOutputFolder(outputFolderField.getText());
    }
    
    public void readBehaviourSettings() {
        portTextField.setText(Integer.toString(appSettings.getPort()));
        removeFinishedCheckBox.setSelected(appSettings.isRemoveFinished());
        keepServerTimeCheckBox.setSelected(appSettings.isKeepServerTime());
        askOnExitCheckBox.setSelected(appSettings.isAskBeforeExit());
        askOnDeleteCheckBox.setSelected(appSettings.isAskBeforeRemove());
        autostartDownloadCheckBox.setSelected(appSettings.isAutostartDownloads());
        silentDropCheckBox.setSelected(!appSettings.isSilentDrop());
        addToFrontCheckBox.setSelected(appSettings.isAddToFront());
        backupListCheckBox.setSelected(appSettings.isNeedListBackuping());
        alowDuplicatesCheckBox.setSelected(appSettings.isAlowDuplicates());
    }
    
    public void saveBehaviourSettings() {
        appSettingsWriter.setPort(Integer.parseInt(portTextField.getText()));
        appSettingsWriter.setRemoveFinished(removeFinishedCheckBox.isSelected());
        appSettingsWriter.setKeepServerTime(keepServerTimeCheckBox.isSelected());
        appSettingsWriter.setAskBeforeExit(askOnExitCheckBox.isSelected());
        appSettingsWriter.setAskBeforeRemove(askOnDeleteCheckBox.isSelected());
        appSettingsWriter.setAutostartDownloads(autostartDownloadCheckBox.isSelected());
        appSettingsWriter.setSilentDrop(!silentDropCheckBox.isSelected());
        appSettingsWriter.setAddToFront(addToFrontCheckBox.isSelected());
        appSettingsWriter.setNeedListBackuping(backupListCheckBox.isSelected());
        appSettingsWriter.setAlowDuplicates(alowDuplicatesCheckBox.isSelected());
    }
    
    public void readLanguageSettings() {
        if (appSettings.isUseSystemLocale()) {
            useSystemLocaleCheckBox.setSelected(true);
        }else{
            languageComboBox.setEnabled(true);
            useSystemLocaleCheckBox.setSelected(false);
        }
        
        if (appSettings.getCustomLocale() != null) {
            languageComboBox.setSelectedItem(appSettings.getCustomLocale());
        }
    }
    
    public void readIntegrationSettings() {
        konquerorCheckBox.setSelected(appSettings.isKonquerorIntegrated());
        firefoxCheckBox.setSelected(appSettings.isFirefoxIntegrated());
    }
    
    public void saveIntegraionSettings() {
        // current box state:
        boolean isCheckBoxSelected = konquerorCheckBox.isSelected();
        // if check box is selected now and not selected in settings - integrate now:
        if (isCheckBoxSelected && !appSettings.isKonquerorIntegrated()) {
            // integrating
            IntegrationHelper.addKonqueror(this);
        } else if (!isCheckBoxSelected && appSettings.isKonquerorIntegrated()) {
            // if we were integrated - remove self from integration
            IntegrationHelper.removeKonqueror();
        }
        
        appSettingsWriter.setKonquerorIntegrated(isCheckBoxSelected);
        appSettingsWriter.setFirefoxIntegrated(firefoxCheckBox.isSelected());
    }
    
    public void saveLanguageSettings() {
        // TODO in future we will need better locale system
        /*
        boolean useSystemLocale = useSystemLocaleCheckBox.isSelected();
        String customLocale = languageComboBox.getSelectedItem().toString();
        if (!useSystemLocale && !customLocale.equals(appSettings.getCustomLocale())) {
            if (customLocale.equals(Constants.ENGLISH_LNG)) {
                Locale.setDefault(Locale.US);
            } else if (customLocale.equals(Constants.RUSSIAN_LNG)) {
                Locale.setDefault(new Locale("ru", "RU"));
            }
            ResourceBundle.clearCache();
        } else if (useSystemLocale && !appSettings.isUseSystemLocale()) {
            Locale.setDefault(Locale.getDefault());
            ResourceBundle.clearCache();
        }
         */
        
        appSettingsWriter.setUseSystemLocale(useSystemLocaleCheckBox.isSelected());
        appSettingsWriter.setCustomLocale(languageComboBox.getSelectedItem().toString());
    }
    
    public void readHistorySettings() {
        enableHistoryCheckBox.setSelected(appSettings.isEnableHistory());
        notifyIfFoundInHitoryCheckBox.setSelected(appSettings.isNotifyDownloadsInHistory());
        historyTimespanSpinner.setValue(appSettings.getStoreHistoryDays());
        historySizeLimitSpinner.setValue(appSettings.getHistorySizeLimitKb());
        enableHistoryCheckBoxActionPerformed(null);
    }

    public void saveHistorySettings() {
        appSettingsWriter.setEnableHistory(enableHistoryCheckBox.isSelected());
        appSettingsWriter.setNotifyDownloadsInHistory(notifyIfFoundInHitoryCheckBox.isSelected());
        appSettingsWriter.setStoreHistoryDays((Integer)historyTimespanSpinner.getValue());
        appSettingsWriter.setHistorySizeLimitKb((Integer)historySizeLimitSpinner.getValue());
    }
    
    // </editor-fold>
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox addToFrontCheckBox;
    private javax.swing.JCheckBox alowDuplicatesCheckBox;
    private javax.swing.JCheckBox askOnDeleteCheckBox;
    private javax.swing.JCheckBox askOnExitCheckBox;
    private javax.swing.JCheckBox autostartDownloadCheckBox;
    private javax.swing.JCheckBox backupListCheckBox;
    private javax.swing.JPanel behaviourPanel;
    private org.tiberiumlabs.lailaps.gui.elements.OpenButton browseListFile;
    private org.tiberiumlabs.lailaps.gui.elements.OpenButton browseOutputFolder;
    private javax.swing.JLabel customLanguageLabel;
    private javax.swing.JCheckBox enableHistoryCheckBox;
    private javax.swing.JPanel enviromentPanel;
    private javax.swing.JCheckBox firefoxCheckBox;
    private javax.swing.JLabel firefoxImage;
    private javax.swing.JLabel historyDaysLabel;
    private javax.swing.JLabel historyKilobytesLabel;
    private javax.swing.JPanel historyPanel;
    private javax.swing.JLabel historySizeLimitLabel;
    private javax.swing.JSpinner historySizeLimitSpinner;
    private javax.swing.JLabel historyTimespanLabel;
    private javax.swing.JSpinner historyTimespanSpinner;
    private javax.swing.JPanel integrationPanel;
    private javax.swing.JPanel interceptFromPanel;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JCheckBox keepServerTimeCheckBox;
    private javax.swing.JCheckBox konquerorCheckBox;
    private javax.swing.JLabel konquerorImage;
    private javax.swing.JComboBox languageComboBox;
    private javax.swing.JPanel languagePanel;
    private javax.swing.JTextField listFileTextField;
    private javax.swing.JPanel listPanel;
    private javax.swing.JLabel noLimitLabel1;
    private javax.swing.JLabel noLimitLabel2;
    private javax.swing.JCheckBox notifyIfFoundInHitoryCheckBox;
    private javax.swing.JTextField outputFolderField;
    private javax.swing.JPanel outputPanel;
    private javax.swing.JLabel portLabel;
    private javax.swing.JTextField portTextField;
    private javax.swing.JCheckBox removeFinishedCheckBox;
    private javax.swing.JTextField settingsDirTextField;
    private javax.swing.JPanel settingsPanel;
    private javax.swing.JCheckBox silentDropCheckBox;
    private javax.swing.JCheckBox useSystemLocaleCheckBox;
    // End of variables declaration//GEN-END:variables
    
}
